package com.lecture.java8.gui;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class getAllData {
    public static ArrayList<Integer> getOneData(int date, String name) {
        Connection conn = null;
        Statement stmt = null;
        ArrayList<Integer> id=new ArrayList<Integer>();
        try {
            conn = getConnection.getConnection();
            //数据库编写sql语句
            stmt = conn.createStatement();
            String sql = "select * from timetable where date="+date+" and name= '"+name+"';";
            ResultSet rs = stmt.executeQuery(sql);
            System.out.println("————sql已经执行!");
            while (rs.next()) {
                id.add(rs.getInt("id"));
            }
        } catch (Exception e) {
            System.out.println("出现异常:" + e);
        } finally {
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return id;
    }

    public static void main(String[] args) {
        Connection conn=null;
        Statement stmt=null;
        try {
            conn= getConnection.getConnection();
            //数据库编写sql语句
            stmt=conn.createStatement();
            String sql="select* from users;";
            ResultSet rs=stmt.executeQuery(sql);
            System.out.println("————sql已经执行!");
            ArrayList<User> usersList=new ArrayList();
            //解析结果集
            while (rs.next()){
                User u = new User(rs.getInt("date"), rs.getInt("id"),rs.getString("name"), rs.getString("starting"),rs.getInt("lasting"));
                usersList.add(u);
            }
            for (User u:usersList){
                System.out.println(u.getDate());
                System.out.println(u.getId());
                System.out.println(u.getName());
                System.out.println(u.getStarting());
                System.out.println(u.getLasting());
            }
        } catch (Exception e) {
            System.out.println("出现异常:"+e);
        }finally{
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
